Option Explicit On

Public Const nomMenu As String = "MiMenu"

Sub BorrarMenu()
    ' Borra el men por si ste existe previamente
    On Error Resume Next
    Application.CommandBars(nomMenu).Delete()
    On Error GoTo 0
End Sub

Sub CrearMenu()
    ' Borra el men.
    Call BorrarMenu()

    ' Crea el men.
    Call MenuPersonalizado()

    ' Muestra el men.
    On Error Resume Next
    Application.CommandBars(nomMenu).ShowPopup()
    On Error GoTo 0
End Sub

Sub MenuPersonalizado()
    Dim MenuItem As CommandBarPopup
    With Application.CommandBars.Add(Name:=nomMenu, Position:=msoBarPopup, _
        MenuBar:=False, Temporary:=True)

        With .Controls.Add(Type:=msoControlButton)
            .Caption = "Calculadora"
            .FaceId = 71
            .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_calculadora"
        End With

        With .Controls.Add(Type:=msoControlButton)
            .Caption = "Saludo"
            .FaceId = 72
            .OnAction = "'" & ThisWorkbook.Name & "'!" & "saludo"
        End With

        MenuItem = .Controls.Add(Type:=msoControlPopup)
        With MenuItem
            .Caption = "Formularios"

            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Mens de tipo 2"
                .FaceId = 71
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_menus2"
            End With

            With .Controls.Add(Type:=msoControlButton)
                .Caption = "Mens de tipo 3"
                .FaceId = 72
                .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_menus3"
            End With
            MenuItem = .Controls.Add(Type:=msoControlPopup)
            With MenuItem
                .Caption = "Mas submenus"

                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Calculadora"
                    .FaceId = 71
                    .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_calculadora"
                End With

                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Notepad"
                    .FaceId = 72
                    .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_notepad"
                End With
                With .Controls.Add(Type:=msoControlButton)
                    .Caption = "Explorador de Windows"
                    .FaceId = 73
                    .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_explorer"
                End With

            End With

        End With
        With .Controls.Add(Type:=msoControlButton)
            .Caption = "Salir"
            .FaceId = 73
            .OnAction = "'" & ThisWorkbook.Name & "'!" & "lanza_salida"
        End With
    End With
End Sub

Sub TestMacro()
    MsgBox("Hi there! Greetings from the Netherlands.")
End Sub

Sub saludo()
    MsgBox("Hola Mundo!!")
End Sub

Sub lanza_calculadora()
    Shell("calc.exe")
End Sub

Sub lanza_explorer()
    Shell("explorer.exe")
End Sub

Sub lanza_notepad()
    Shell("Notepad.exe", windowstyle:=VbAppWinStyle.vbNormalFocus)
End Sub

Sub lanza_menus2()
    frmMenus2.Show()
End Sub

Sub lanza_menus3()
    frmMenus3.Show()
End Sub

Sub lanza_salida()
    Unload(frmMenuPral)
End Sub
